<?php

/**
 * adminstores actions.
 *
 * @package    retailbrain
 * @subpackage adminstores
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class adminstoresActions extends sfActions {

    /**
     * Executes channels action
     *
     * @param sfRequest $request A request object
     */
    public function executeIndex(sfWebRequest $request) {

        $this->getResponse()->setSlot('title', 'Administración : Locales');
        $this->breadcrumbs = array(array("Administración", "admin/index"), array("Locales", "adminstores/index"));

        $q = Doctrine_Query::create()
                ->from('Stores s')
                ->innerJoin('s.Channels ch')
                ->innerJoin('s.Customers c')
                ->innerJoin('s.Formats f')
                ->innerJoin('s.CodeFormats cf')
                ->innerJoin('s.Concessions cc')
                ->innerJoin('s.Comunas co')
                ->innerJoin('s.Provincias p')
                ->innerJoin('s.Regiones r')
                ->innerJoin('s.Paises pa')
                ->innerJoin('s.Clusters cl')
                ->orderBy('s.name desc');

        $this->stores = $q->fetchArray();

        $this->error = 0;
        $this->dataError = null;

        $this->caps = $this->getUser()->getAttribute('caps');
    }

    /**
     * Executes addChannels action
     *
     * @param sfRequest $request A request object
     */
    public function executeAddStores(sfWebRequest $request) {

        $this->getResponse()->setSlot('title', 'Administración : Agregar Locales');
        $this->breadcrumbs = array(array("Administración", "admin/index"), array("Locales", "adminstores/index"), array("Agregar Locales", "adminstores/addStores"));
    }

    public function executeUploadStores(sfWebRequest $request) {
        $this->isFile = false;
        $this->isUploaded = false;

        if (isset($_FILES["archivo"])) {
            try {
                $this->isFile = true;
                $files = $request->getFiles("archivo");

                $fileName = trim($files["name"]);
                $fileName = md5($fileName . time(date("dmYHis"))) . '.xlsx';
                $path = sfConfig::get("sf_upload_dir") . '/excel';
                if (move_uploaded_file($files["tmp_name"], $path . "/$fileName")) {
                    $this->isUploaded = true;
                }
            } catch (Exception $e) {
                syslog(LOG_ERR, "Error al subir el archivo: " . $e->getMessage());
            }
        } else {
            
        }

        $objReader = new PHPExcel_Reader_Excel2007();
        $objPHPExcel = $objReader->load($path . '/' . $fileName);


        $sheetData = $objPHPExcel->getActiveSheet()->toArray(true, true, true, true);

        $data = array();
        for ($i = 2; $i <= count($sheetData); $i++) {

            $data[$i] = loadLocales::validateExcelLocales($i, $sheetData[$i]);
        }


        $error = 0;
        foreach ($data as $key => $value) {
            if ($value[$key] != "") {
                $error++;
            }
        }

        if ($error == 0) {

            for ($i = 2; $i <= count($sheetData); $i++) {

                loadLocales::insertExcelLocales($i, $sheetData[$i]);
            }
        }

        $this->error = $error;
        $this->dataError = $data;

        $this->getResponse()->setSlot('title', 'Administración : Locales');
        $this->breadcrumbs = array(array("Administración", "admin/index"), array("Locales", "adminstores/index"));

        $q = Doctrine_Query::create()
                ->from('Stores s')
                ->innerJoin('s.Channels ch')
                ->innerJoin('s.Customers c')
                ->innerJoin('s.Formats f')
                ->innerJoin('s.CodeFormats cf')
                ->innerJoin('s.Concessions cc')
                ->innerJoin('s.Comunas co')
                ->innerJoin('s.Provincias p')
                ->innerJoin('s.Regiones r')
                ->innerJoin('s.Paises pa')
                ->innerJoin('s.Clusters cl')
                ->orderBy('s.name desc');

        $this->stores = $q->fetchArray();
        $this->setTemplate('stores');
    }

    
    
    /**
     * Executes editStore action
     *
     * @param sfRequest $request A request object
     */
    public function executeEditStore(sfWebRequest $request) {

        $id = $this->getRequestParameter('id');

        $this->getResponse()->setSlot('title', 'Administración : Editar Local');
        $this->breadcrumbs = array(array("Administración", "admin/index"),array("Locales", "adminstore/index"), array("Editar Local", "adminstore/editStore?id=$id"));
        
        
        /** Stores **/
        $q = Doctrine_Query::create()
                ->from('Stores s')
                ->where('s.id_store = ?', array($id))
                ->fetchArray();

        $this->store = $q;
        
        /** Channels **/
        $q = Doctrine_Query::create()
                ->from('Channels c')
                ->fetchArray();
        $this->channels = $q;
        
        /** Customers **/
        $q = Doctrine_Query::create()
                ->from('Customers c')
                ->fetchArray();
        $this->customers = $q;
        
        /** Formats **/
        $q = Doctrine_Query::create()
                ->from('Formats f')
                ->fetchArray();
        $this->formats = $q;
        
        /** CodeFormats **/
        $q = Doctrine_Query::create()
                ->from('CodeFormats c')
                ->fetchArray();
        $this->codeFormats = $q;
        
        /** Concessions **/
        $q = Doctrine_Query::create()
                ->from('Concessions c')
                ->fetchArray();
        $this->concessions = $q;
        
        /** Clusters **/
        $q = Doctrine_Query::create()
                ->from('Clusters c')
                ->fetchArray();
        $this->clusters = $q;
        
        /** Paises **/
        $q = Doctrine_Query::create()
                ->from('Paises p')
                ->fetchArray();
        $this->paises = $q;
        
        /** Regiones **/
        $q = Doctrine_Query::create()
                ->from('Regiones r')
                ->fetchArray();
        $this->regiones = $q;
        
        /** Provincias **/
        $q = Doctrine_Query::create()
                ->from('Provincias p')
                ->fetchArray();
        $this->provincias = $q;
        
        /** Comunas **/
        $q = Doctrine_Query::create()
                ->from('Comunas c')
                ->fetchArray();
        $this->comunas = $q;
        
        /** Locations **/
        $q = Doctrine_Query::create()
                ->from('Locations l')
                ->fetchArray();
        $this->locations = $q;
        
    }
    
    
    
    
    
    /**
     * Executes updateStore action
     * 
     * @param sfWebRequest $request
     * @return type 
     */
    public function executeUpdateStore(sfWebRequest $request) {

        // Obtengo Datos del Formulario
        $_arrayPost = $request->getParameter('store');
        
        try {

            $tabla = Doctrine::getTable('Stores')->find(array($_arrayPost["id"]));

            $tabla->id_channel        = $_arrayPost['channels'];
            $tabla->id_customer        = $_arrayPost['customers'];
            $tabla->id_format        = $_arrayPost['formats'];
            $tabla->id_codeformat        = $_arrayPost['codeFormats'];
            $tabla->id_concession        = $_arrayPost['concessions'];
            $tabla->id_comuna        = $_arrayPost['comunas'];
            $tabla->id_provincia        = $_arrayPost['provincias'];
            $tabla->id_region        = $_arrayPost['regiones'];
            $tabla->id_pais        = $_arrayPost['paises'];
            $tabla->id_cluster        = $_arrayPost['clusters'];
            $tabla->name        = $_arrayPost['name'];
            $tabla->code_supplier        = $_arrayPost['code_supplier'];
            $tabla->surface        = $_arrayPost['surface'];
            $tabla->id_location        = $_arrayPost['locations'];
            $tabla->address        = $_arrayPost['address'];
            $tabla->responsiblea        = $_arrayPost['responsiblea'];
            $tabla->responsibleb        = $_arrayPost['responsibleb'];
            $tabla->responsiblec        = $_arrayPost['responsiblec'];
            $tabla->manager        = $_arrayPost['manager'];
            $tabla->administrator        = $_arrayPost['administrator'];
            $tabla->average_sales        = $_arrayPost['average'];
            $tabla->latitude        = $_arrayPost['latitude'];
            $tabla->longitude       = $_arrayPost['longitude'];

            $tabla->save();
        } catch (Exception $e) {
            syslog(LOG_ERR, "Error: " . $e->getMessage());
        }

        $this->redirect('/adminstores/index');
    }
    
    
}
